package com.info33.platform.system.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.info33.platform.common.exception.TransactionalException;
import com.info33.platform.system.dao.SysUserEducationDao;
import com.info33.platform.system.dto.SysUserEducationDTO;
import com.info33.platform.system.entity.SysUserEducation;
import com.info33.platform.system.service.SysUserEducationService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author admin
 */
@Service
public class SysUserEducationServiceImpl extends ServiceImpl<SysUserEducationDao, SysUserEducation> implements SysUserEducationService {

    @Autowired
    private SysUserEducationDao sysUserEducationDao;

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void judgmentAdd(List<SysUserEducationDTO> list, Long uid) {
        if (ObjectUtils.isEmpty(list)) {
            return;
        }
        List<SysUserEducation> saveList = new ArrayList<>();
        list.forEach(l -> {
            SysUserEducation education = new SysUserEducation();
            BeanUtils.copyProperties(l, education);
            education.setUserId(uid);
            saveList.add(education);
        });
        if (Boolean.FALSE.equals(this.saveOrUpdateBatch(saveList))) {
            throw new TransactionalException();
        }
    }
}